package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AjaxController {


    @RequestMapping("/ajaxController")
    //使用@RequestBody注解，可以将请求体内容封装到指定参数中
    public String ajaxController(@RequestBody String message){
        System.out.println("ajax request is running..."+message);
        return "page.jsp";
    }


    @RequestMapping("/ajaxPojoToController")
    public String ajaxPojoToController(@RequestBody User user){
        System.out.println("controller pojo:"+user);
        return "page.jsp";

    }

    @RequestMapping("/ajaxListToController")
      public String ajaxListToController(@RequestBody List<User> userList){
      System.out.println("controller list:"+userList);
      return "page.jsp";
    }


    @RequestMapping("/ajaxReturnString")
    public @ResponseBody String ajaxReturnString(){
        System.out.println("controller return string ...");
        return "page.jsp";
    }


    @RequestMapping("/ajaxReturnJson")
    @ResponseBody
    public User ajaxReturnJson(){
        System.out.println("controller return json pojo...");
        User user=new User();
        user.setName("Jockme");
        user.setAge(40);
        return user;
    }

    @RequestMapping("ajaxReturnJsonList")
    @ResponseBody
    public List ajaxReturnJsonList(){
        System.out.println("controller return json list...");
        User user1=new User();
        user1.setName("tom");
        user1.setAge(12);

        User user2=new User();
        user2.setName("jieni");
        user2.setAge(13);

        ArrayList a1=new ArrayList();
        a1.add(user1);
        a1.add(user2);
        return a1;

    }


    @RequestMapping("/cross")
    @ResponseBody
    @CrossOrigin
    public User cross(HttpServletRequest request){
        System.out.println("controller cross..."+request.getRequestURI());
        User user=new User();
        user.setName("jjjj");
        user.setAge(22);
        return user;
    }




}
